#include"state.h"

void ArmorState::startProgram(Work w , Serial serial,Mat src) 
{
     if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::ARMOR_SHOOT )
    {
        if(taskFactory == nullptr)
        {
            taskFactory = make_shared<ArmorShootFactory>();
            taskOperation = taskFactory->creatTask();
        }
        taskOperation->setImg(src);
        Point2f center = taskOperation->shootCenter();
        cout << center<<endl;
        if(center==Point2f(0,0))
        {
                uint8_t buff[17];
                buff[0] = 's';
                for(int i=1; i<16;i++)
                {
                    buff[i] = '0';
                }
                buff[16] = 'e';
                serial.WriteData(buff, sizeof(buff));
        }
        else
        {
                circle(src,center,3,Scalar(0, 255, 0),2);
                serial.sendBoxPosition(center,serial,1,0);
        }
    }
    else if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::BUFF_SHOOT)
    {
       w.setState(shared_ptr<BuffState>(new BuffState()));
       w.startProgram(serial,src);
    }
    else
    {
        w.setState(shared_ptr<NoState>(new NoState()));
        w.startProgram(serial,src);
    }
}

void BuffState::startProgram(Work w , Serial serial,Mat src) 
{
    if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::BUFF_SHOOT)
    {
        if(taskFactory == nullptr)
        {
            taskFactory = make_shared<BuffShootFactory>();
            taskOperation = taskFactory->creatTask();
        }
        taskOperation->setImg(src);
        Point2f center = taskOperation->shootCenter();
    }
    else if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::ARMOR_SHOOT)
    {
        w.setState(shared_ptr<ArmorState>(new ArmorState()));
        w.startProgram(serial,src);
    }
    else
    {
        w.setState(shared_ptr<NoState>(new NoState()));
        w.startProgram(serial,src);
    }
    
}

void NoState::startProgram(Work w,Serial serial,Mat src) 
{
    if(w._task == Serial::NO_TASK)
    {
        if(taskFactory == nullptr)
        {
            taskFactory = make_shared<NoTaskFactory>();
            taskOperation = taskFactory->creatTask();
        }
    }
    else if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::ARMOR_SHOOT)
    {
        w.setState(shared_ptr<ArmorState>(new ArmorState()));
        w.startProgram(serial,src);
    }
    else
    {
        w.setState(shared_ptr<BuffState>(new BuffState()));
        w.startProgram(serial,src);
    }
}

void Work::setState(shared_ptr<State>  s)
{
    this ->state  = s;
}